#include<cstdio>
#include<algorithm>
#include<cstring>
#define openfile(s) freopen(s".in","s",stdin);freopen(s".out","w",stdout)
using namespace std;
const int N=1e3+1;
const long long MOD=1e9+7;
long long mod(long long a,long long b=MOD)
{
	a-=a/b*b;
	return a<0?a+b:a;
}
int n,a[N],b[N];
long long g[N][N],f[N]; 
void work()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i]);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i==1 && j==1)
			{
				g[j][0]=1;
				continue;
			}
			f[0]=(g[j][0]+g[j-1][0]-g[j-1][1]*(a[i]==b[j])+MOD)%MOD;
			for(int k=1;i-k>0 || j+k<=n;k++)
			{
				f[k]=(g[j][k-1]*(i-k>0 && a[i-k]==b[j])+g[j-1][k+1]*(j+k<=n && a[i]==b[j+k]))%MOD;
			}
			memcpy(g[j],f,(n+1)*sizeof(long long));
		}
	}
	printf("%lld",g[n][0]);
	return;
}
int main()
{
	freopen("data.txt","r",stdin);
	openfile("zha");
	work();
	return 0;
}
